﻿using Itech_Run.EntityFrameworkCore.Shared.MigrationHelper;
using Itech_Run.Helper.Configure;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration;

namespace Itech_Run.Blog.EntityFrameworkCore.MigrationHelper;

/// <summary>
/// 设计时 DbContext 创建：https://docs.microsoft.com/zh-cn/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli
/// </summary>
internal class CustomContextFactory : IDesignTimeDbContextFactory<Itech_RunBlogContext>
{
    public Itech_RunBlogContext CreateDbContext(string[] args)
    {
        var configuration = BuildConfiguration.Build();

        var optionsBuilder = new DbContextOptionsBuilder<Itech_RunBlogContext>();
        //Custom Migrations Operations：https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/operations
        optionsBuilder.UseSqlServer(configuration.GetConnectionString("Itech_RunConnectionStr"))
            .ReplaceService<IMigrationsSqlGenerator, CustomSqlServerMigrationsSqlGenerator>();
        return new Itech_RunBlogContext(optionsBuilder.Options);
    }
}